/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.smn.dao;

import com.smn.domain.MyUser;
import com.smn.domain.Share;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author akefeli
 */
@Stateless
public class MyUserFacade extends AbstractFacade<MyUser> {
    @PersistenceContext(unitName = "sharemenowPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public MyUserFacade() {
        super(MyUser.class);
    }
    public MyUser findUser(String username){
           try {
            Query cq = (Query) getEntityManager().createQuery("SELECT m FROM MyUser m WHERE m.userName=:username");
            cq.setParameter("username", username);
            return (MyUser) cq.getSingleResult();
        } catch (NoResultException e) {
            e.toString();
            return null;
        }

    }
    
    public List<MyUser> findUsersForShare(Share share){
           try {
            Query cq = (Query) getEntityManager().createQuery("SELECT m FROM MyUser m,Ticket t WHERE t.referer=m AND t.share=:share");
            cq.setParameter("share", share);
            return cq.getResultList();
        } catch (Exception e) {
               System.out.println(e.getLocalizedMessage());
            return null;
        }

        
    }
    
    public List<MyUser> findAcceptedUsersForShare(Share share){
           try {
            Query cq = (Query) getEntityManager().createQuery("SELECT m FROM MyUser m,Ticket t WHERE t.referer=m AND t.share=:share AND t.accepted=true");
            cq.setParameter("share", share);
            return cq.getResultList();
        } catch (Exception e) {
               System.out.println(e.getLocalizedMessage());
            return null;
        }

        
    }
    
}
